package com.thb.pay.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.vo.req.AgentpayPassageAccountPageReq;
import com.thb.pay.api.payment.vo.req.AgentpayPassageAccountReq;
import com.thb.pay.api.payment.vo.resp.AgentpayPassageAccountVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.AgentpayPassageAccount;
import com.thb.pay.payment.mapper.AgentpayPassageAccountMapper;
import com.thb.pay.payment.service.AgentpayPassageAccountService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 代付-通道-账户表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class AgentpayPassageAccountServiceImpl extends BaseServiceImpl<AgentpayPassageAccountMapper, AgentpayPassageAccount> implements AgentpayPassageAccountService {

    @Override
    public PageResult<AgentpayPassageAccountVo> findPage(AgentpayPassageAccountPageReq pageReq) {
        IPage<AgentpayPassageAccount> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<AgentpayPassageAccount> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, AgentpayPassageAccountVo.class);
    }

    @Override
    public void add(AgentpayPassageAccountReq agentpayPassageAccountReq) {
        Assert.notNull(agentpayPassageAccountReq, "agentpayPassageAccountReq.not.null");
        Assert.notNull(agentpayPassageAccountReq.getAgentpayPassageId(), "agentpayPassageId.not.null");
        AgentpayPassageAccount agentpayPassageAccount = new AgentpayPassageAccount();
        BeanUtil.copyProperties(agentpayPassageAccountReq, agentpayPassageAccount);
        super.insert(agentpayPassageAccount);
    }

    @Override
    public void update(AgentpayPassageAccountReq agentpayPassageAccountReq) {
        Assert.notNull(agentpayPassageAccountReq, "agentpayPassageAccountReq.not.null");
        Assert.notNull(agentpayPassageAccountReq.getId(), "agentPassageAccountId.not.null");
        AgentpayPassageAccount agentpayPassageAccount = super.selectById(agentpayPassageAccountReq.getId());
        Assert.notNull(agentpayPassageAccount, "agentpayPassageAccount.not.null");
        AgentpayPassageAccount targetAgentpayPassageAccount = new AgentpayPassageAccount();
        BeanUtil.copyProperties(agentpayPassageAccountReq, targetAgentpayPassageAccount);
        super.updateById(targetAgentpayPassageAccount);
    }

    @Override
    public AgentpayPassageAccountVo getById(Long id) {
        Assert.notNull(id, "agentPassageAccountId.not.null");
        AgentpayPassageAccount agentpayPassageAccount = super.selectById(id);
        if (agentpayPassageAccount != null) {
            AgentpayPassageAccountVo agentpayPassageAccountVo = new AgentpayPassageAccountVo();
            BeanUtil.copyProperties(agentpayPassageAccount, agentpayPassageAccountVo);
            return agentpayPassageAccountVo;
        }
        return null;
    }

    @Override
    public List<AgentpayPassageAccountVo> findAllList(AgentpayPassageAccountReq conditionParam) {
        List<AgentpayPassageAccountVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentpayPassageAccount> queryWrapper = new LambdaQueryWrapper<>();
        if (conditionParam != null) {
            if (conditionParam.getAgentpayPassageId() != null) {
                queryWrapper.eq(AgentpayPassageAccount::getAgentpayPassageId, conditionParam.getAgentpayPassageId());
            }
            if (conditionParam.getRiskStatus() != null && conditionParam.getRiskStatus() != -99) {
                queryWrapper.eq(AgentpayPassageAccount::getRiskStatus, conditionParam.getRiskStatus());
            }
            if (conditionParam.getStatus() != null && conditionParam.getStatus() != -99) {
                queryWrapper.eq(AgentpayPassageAccount::getStatus, conditionParam.getStatus());
            }
        }
        queryWrapper.orderByDesc(AgentpayPassageAccount::getCreateTime);
        List<AgentpayPassageAccount> resResultList = super.selectList(queryWrapper);
        copyList(targetResultList, resResultList);
        return targetResultList;
    }

    @Override
    public List<AgentpayPassageAccountVo> findAllByPassageId(Integer agentpayPassageId) {
        List<AgentpayPassageAccountVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<AgentpayPassageAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgentpayPassageAccount::getAgentpayPassageId, agentpayPassageId);
        List<AgentpayPassageAccount> resResultList = super.selectList(queryWrapper);
        copyList(targetResultList, resResultList);
        return targetResultList;
    }

    private void copyList(List<AgentpayPassageAccountVo> targetResultList, List<AgentpayPassageAccount> resResultList) {
        if (CollectionUtil.isNotEmpty(resResultList)) {
            resResultList.forEach(agentpayPassageAccount -> {
                AgentpayPassageAccountVo agentpayPassageAccountVo = new AgentpayPassageAccountVo();
                BeanUtil.copyProperties(agentpayPassageAccount, agentpayPassageAccountVo);
                targetResultList.add(agentpayPassageAccountVo);
            });
        }
    }
}
